#include "cepollclient.h"

CEpollClient::CEpollClient(int iUserCount, const char* pIP, int iPort)
{
    strcpy(m_ip, pIP);
    m_iPort = iPort;
    m_iUserCount = iUserCount;
    m_iEpollFd = epoll_create(_MAX_SOCKFD_COUNT);
    m_pAllUserStatus = (struct UserStatus*)malloc(iUserCount*sizeof(struct UserStatus));
    for (int iuserid = 0; iuserid < iUserCount; iuserid++)
    {
        m_pAllUserStatus[iuserid].iUserStatus = FREE;
        sprintf(m_pAllUserStatus[iuserid].cSendbuff, "Client: %d send message \"Hello Server!\" \r\n", iuserid);
        m_pAllUserStatus[iuserid].iBuffLen = strlen(m_pAllUserStatus[iuserid].cSendbuff) + 1;
        m_pAllUserStatus[iuserid].iSockFd = -1;
    }
    memset(m_iSockFd_UserId, oxFF, sizeof(m_iSockFd_UserId));
}

CEpollClient::~CEpollClient()
{
    free(m_pAllUserStatus);
}

int CEpollClient::ConnectToServer(int iUserId, const char* pServerIp, unsigned short uServerPort)
{
    if ((m_pAllUserStatus[iUserId].iSockFd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        std::cout << "[CEpollClient error]: init sockt fail, the reason is :" << strerror(errno) << ",errno is :" << errno << std::endl;
        m_pAllUserStatus[iUserId].iSockFd = -1;
        return m_pAllUserStatus[iUserId].iSockFd;
    }

    struct sockaddr_in addr;
    bzero(&addr, sizeof(addr));

    addr.sin_family = AF_INET;
    addr.sin_port = htons(uServerPort);
    add.sin_addr.s_addr = inet_addr(pServerIp);

    int ireuseadd_on = 1;
    setsockopt(m_pAllUserStatus[iUserId].iSockFd, SOL_SOCKET, SO_REUSEADDR, &ireuseadd_on, sizeof(ireuseadd_on));

    unsigned long ul = 1;
    ioctl(m_pAllUserStatus[iUserId].iSockFd, FIONBIO, &ul);

    connect(m_pAllUserStatus[iUserId].iSockFd, (const sockaddr*)&addr, sizeof(addr));
    m_pAllUserStatus[]
}
